package cn.tedu.homework_weibo.controller;

import cn.tedu.homework_weibo.common.JsonResult;
import cn.tedu.homework_weibo.common.StatusCode;
import cn.tedu.homework_weibo.mapper.CommentMapper;
import cn.tedu.homework_weibo.pojo.dto.CommentDTO;
import cn.tedu.homework_weibo.pojo.entity.Comment;
import cn.tedu.homework_weibo.pojo.vo.CommentVO;
import cn.tedu.homework_weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.评论模块")
@RestController
@Slf4j
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @ApiOperation("发布评论")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUser_id(userVO.getId());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }
    @ApiOperation("获取评论列表")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(@RequestParam Integer id){
        List<CommentVO> commentVOList = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentVOList);

    }

}
